package com.example.joker.abao00;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.joker.abao00.HttpUtil.HttpCallbackListener;
import com.example.joker.abao00.HttpUtil.HttpUtil;
import com.example.joker.abao00.HttpUtil.Utility;
import com.example.joker.abao00.model.Msg;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joker on 2015/12/2.
 */
public class ChatRobot extends Activity{
    private ListView msgListView;
    private EditText inputText;
    private Button send;
    private MessageAdapter adapter;
    private List<Msg> msgList=new ArrayList<>();
    private static String API_KEY="980ae7be45f8362b2234609945b53453";
    private static final String TAG="testhttp";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_chat);
        Msg initMsg=new Msg();
        initMsg.setReplyText("你好,我是阿宝,很高兴和你聊天.你可以问我任何城市天气,讲个笑话,列车查询,菜谱信息等等");
        msgList.add(initMsg);
        adapter=new MessageAdapter(ChatRobot.this,R.layout.msg_item,msgList);
        msgListView=(ListView)findViewById(R.id.msg_list_view);
        msgListView.setAdapter(adapter);
        inputText=(EditText)findViewById(R.id.input_text);
            send = (Button) findViewById(R.id.send);
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String questionText = inputText.getText().toString();
                    Msg questionMsg = new Msg();
                    questionMsg.setType(Msg.TYPE_SEND);
                    questionMsg.setQuestionText(questionText);
                    msgList.add(questionMsg);
                    adapter.notifyDataSetChanged();
                    msgListView.setSelection(msgList.size());
                    inputText.setText("");
                    queryFromServer(questionText);
                    Log.d("replyserver", questionText);
                }
            });
    }

    private void queryFromServer(String questionText){
        Log.d(TAG, "queryFromServer text", new Exception());
        String address;

        try
        {
            questionText= URLEncoder.encode(questionText, "UTF-8");
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        if(!TextUtils.isEmpty(questionText)){
        address="http://www.tuling123.com/openapi/api?key="+API_KEY+"&info="+questionText;}
        else{
            address="http://www.tuling123.com/openapi/api";

        }
        Log.d("address", address);

        HttpUtil.httpSendRequest(address, new HttpCallbackListener() {


            @Override
            public void onFinish(String response) {
                final Msg replyMsg= Utility.handleReplyFromServer(ChatRobot.this, response);
                Log.d("response1", response);


                if (response!=null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            msgList.add(replyMsg);
                            adapter.notifyDataSetChanged();
                            msgListView.setSelection(msgList.size());//msgList.size()说明会定位在最尾的下一行

                        }
                    });
                }
            }

            @Override
            public void onError(Exception e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Toast.makeText(ChatRobot.this,
                                "亲,您的网络可能未连接哟", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    }


